package com.interview.juc;

import java.io.*;

class User implements Serializable {
    private String username;
    private transient String password;
    private String dept;

    public User(String username, String password, String dept) {
        this.username = username;
        this.password = password;
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", dept='" + dept + '\'' +
                '}';
    }
}


public class TransientDemo {
    public static void main(String[] args) throws Exception {
        writerObject();
        readObject();
    }

    private static void readObject() throws IOException, ClassNotFoundException {
        //包装流和节点流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("account.txt"));
        User user = (User) ois.readObject();
        System.out.println(user);
        ois.close();
    }

    private static void writerObject() throws IOException {
        User account = new User("迪丽热巴","123456","新疆乌鲁木齐");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("account.txt"));
        oos.writeObject(account);
        oos.close();
    }
}
